package com.colorful.insuranceproject.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.PaymentBean;
import com.colorful.insuranceproject.bean.WebViewBean;
import com.colorful.insuranceproject.commonActivity.PaymentActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.HomeRequest;
import com.colorful.insuranceproject.utils.WebViewHelper;
import com.lovely3x.common.managements.user.TokenManager;
import com.lovely3x.common.utils.Response;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/24.
 * <p>
 * 微药店的页面展示
 * <p>
 * http://blog.csdn.net/u010694658/article/details/52388287
 */
public class ColorfulActivity extends TitleActivity {

    private static final int SUBMIT_ORDER_URL = 0X1;

    private HomeRequest homeRequest;

    @Bind(R.id.wb)
    WebView web;

    @Override
    protected int getContentView() {
        return R.layout.activity_wv;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        hiddenTitle();
    }

    @Override
    protected void onViewInitialized() {
        homeRequest = new HomeRequest(getHandler());
        WebSettings settings = web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("/wechat/weixinpays/toPay.html")) {//点击支付的时候做出拦劫
                    Map<String, String> map = WebViewHelper.URLRequest(url);
                    String orderInfo = map.containsKey("orderNo") ? map.get("orderNo") : "";
                    showProgressCircle(getString(R.string.please_wait));
                    String urls = Uri.decode(orderInfo);
                    String isBatch = map.containsKey("isBatch") ? map.get("isBatch") : "";
                    String isBatchs = Uri.decode(isBatch);
                    homeRequest.generateWebOrderUrl(urls, isBatchs, SUBMIT_ORDER_URL);
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissProgressCircle();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showProgressCircle("加载中....");
            }
        });

        //  请求地址的拼接
        String token = TokenManager.getInstance().getTokeValue();
        String mURL = "https://admin.colorfulflorist.com/test/wechat/index/index.html?appToken=" + token;
        web.loadUrl(mURL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case SUBMIT_ORDER_URL:
                dismissProgressCircle();
                if (response.obj != null) {
                    WebViewBean wb = (WebViewBean) response.obj;
                    PaymentBean bean = new PaymentBean();
                    bean.setGoodsMoney(wb.getGoodsMoney());
                    bean.setDeliverMoney(wb.getDeliverMoney());
                    bean.setNeedPay(wb.getNeedPay());
                    bean.setUseScore(wb.getUseScore());
                    bean.setDeliverMoney(wb.getDeliverMoney());
                    bean.setOrderNo(wb.getOrderNo());
                    bean.setOrderNo(wb.getOrderNo());
                    bean.setUserId(wb.getUserId());
                    bean.setIsBatch(wb.getIsBatch());
                    bean.setKekanganEDu(wb.getKekanganEDu());
                    launchActivityForResult(PaymentActivity.class, 10000, PaymentActivity.EXTRA_BEAN_DATA, bean);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick({R.id.tv_close, R.id.tv_return})
    void onClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                finish();
                break;
            case R.id.tv_return:
                web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                web.goBack();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && resultCode == 20001) {
            web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            web.goBack();
        }
    }

    /**
     * 该方法主要作用是充值TintBar的颜色  让沉浸式得到显示
     * 如果该类不需要改方法  可以重写 applyTranslationStatusBarAndNavigationBar   去掉super方法即可
     */
    @Override
    public Tint getTint() {
        Tint tint = super.getTint();
        tint.mStatusBarColor = getResources().getColor(R.color.web_title_color);
        return tint;
    }
}
